package com.guigu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.mapper.BackMapper;
import com.guigu.pojo.*;
import com.guigu.service.BackDetailsService;
import com.guigu.service.BackService;
import com.guigu.service.OrderDetailsService;
import com.guigu.service.OrdersService;
import com.guigu.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-04-07
 */
@CrossOrigin
@Controller
@RequestMapping("/back")
public class BackController {

    @Autowired
    private BackService backService;

    @Autowired
    private BackDetailsService backDetailsService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailsService orderDetailsService;

    @GetMapping("/updateBackTag")
    @ResponseBody
    public Boolean updateBackTag(@RequestParam("orderNumber")String orderNumber,@RequestParam("tag") String backTag){
        Boolean flag =backService.updateBackTag(orderNumber,backTag);
        return flag;
    }
    @PostMapping("/saveBack")
    @ResponseBody
    @Transactional
    public Boolean saveBack(@RequestParam("files") MultipartFile[] files, Back back, HttpServletRequest req) {

        // 设置文件上传的基础路径，并根据日期生成子路径
        String basePathStr = "C:\\Users\\Mr.Zhang\\document\\upload";
        String subPath = FileUploadUtils.makeUploadDirectoryByDate();
        basePathStr += subPath; // 文件上传的地址

        // 检查基础路径是否存在，如果不存在则创建
        File basePath = new File(basePathStr);
        if (!basePath.exists()) {
            basePath.mkdirs();
        }

        try {
            // 遍历上传的文件数组，重命名文件并保存到指定路径
            for (MultipartFile f : files) {
                String newFileName = FileUploadUtils.rename(f.getOriginalFilename());
                f.transferTo(new File(basePath, newFileName));
                back.setImgLink(subPath + newFileName);// 设置用户头像路径
            }
        } catch (Exception e) {
            throw new RuntimeException(e); // 捕获并抛出异常
        }

        back.setCreateDate(new Date());
        back.setBackTag("B001-2");
        // 保存用户信息并返回结果
        QueryWrapper<Back> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",back.getOrderNumber());
        boolean b = backService.saveOrUpdate(back, queryWrapper);
        if (b) {
            Orders orders = ordersService.selectByOrderNo(back.getOrderNumber());
            List<OrderDetails> orderDetailsByOrderId = orderDetailsService.getOrderDetailsByOrderId(orders.getOrderId());
            for (OrderDetails orderDetails : orderDetailsByOrderId){
                BackDetails backDetails = new BackDetails();
                backDetails.setBackId(back.getId());
                backDetails.setProductId(orderDetails.getProductId());
                backDetails.setAmount(orderDetails.getAmount());
                backDetails.setPrice(orderDetails.getPrice());
                backDetails.setSubtotal(orderDetails.getSubtotal());
                backDetails.setBackTag("B001-2");
                backDetails.setBackReason(back.getBackReason());
                backDetails.setBackState("B001-2");
                backDetailsService.save(backDetails);
            }
        }else {
            return false;
        }
        return true;
    }

    @GetMapping("/getbackByorderNumber")
    @ResponseBody
    public Back getbackByorderNumber(@RequestParam("orderNumber") String orderNumber){
        QueryWrapper<Back> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderNumber);
        Back back = backService.getOne(queryWrapper);
        if (back!=null){
            List<BackDetails> backDetails = backDetailsService.getBackDetailsByBackId(back.getId());
            back.setBackDetails(backDetails);
        }
        return back;
    }

    @GetMapping("/acceptanceByOrderNumber")
    @ResponseBody
    public Boolean acceptanceByOrderNumber(@RequestParam("orderNumber") String orderNumber){
        QueryWrapper<Back> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderNumber);
        Back back = backService.getOne(queryWrapper);
        back.setBackTag("B001-3");
        backService.updateById(back);
        for (BackDetails backDetails : backDetailsService.getBackDetailsByBackId(back.getId())){
            backDetails.setBackState("B001-3");
            backDetails.setBackReason("");
            backDetails.setBackTag("B001-3");
            backDetailsService.updateById(backDetails);
        }
        Orders orders = ordersService.selectByOrderNo(orderNumber);
        orders.setBackTag("B001-4");
        ordersService.updateById(orders);
        for (OrderDetails orderDetails : orderDetailsService.getOrderDetailsByOrderId(orders.getOrderId())){
            orderDetails.setBackTag("B001-4");
            orderDetailsService.updateById(orderDetails);
        }
        return true;
    }

    @GetMapping("/reviewByOrderNumber")
    @ResponseBody
    public Boolean reviewByOrderNumber(@RequestParam("orderNumber") String orderNumber){
        QueryWrapper<Back> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderNumber);
        Back back = backService.getOne(queryWrapper);
        back.setBackTag("B001-4");
        backService.updateById(back);
        for (BackDetails backDetails : backDetailsService.getBackDetailsByBackId(back.getId())){
            backDetails.setBackState("B001-4");
            backDetails.setBackReason("");
            backDetails.setBackTag("B001-4");
            backDetailsService.updateById(backDetails);
        }
        Orders orders = ordersService.selectByOrderNo(orderNumber);
        orders.setBackTag("B001-5");
        ordersService.updateById(orders);
        for (OrderDetails orderDetails : orderDetailsService.getOrderDetailsByOrderId(orders.getOrderId())){
            orderDetails.setBackTag("B001-5");
            orderDetailsService.updateById(orderDetails);
        }
        return true;
    }

    @GetMapping("/reasonForReturn")
    @ResponseBody
    public Boolean reasonForReturn(@RequestParam("orderNumber") String orderNumber){
        QueryWrapper<Back> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderNumber);
        Back back = backService.getOne(queryWrapper);
        back.setBackTag("B001-5");
        backService.updateById(back);
        for (BackDetails backDetails : backDetailsService.getBackDetailsByBackId(back.getId())){
            backDetails.setBackState("B001-5");
            backDetails.setBackReason(back.getBackReason());
            backDetails.setBackTag("B001-5");
            backDetailsService.updateById(backDetails);
        }
        Orders orders = ordersService.selectByOrderNo(orderNumber);
        orders.setBackTag("B001-6");
        orders.setBackReason(back.getBackReason());
        ordersService.updateById(orders);
        for (OrderDetails orderDetails : orderDetailsService.getOrderDetailsByOrderId(orders.getOrderId())){
            orderDetails.setBackTag("B001-6");
            orderDetails.setBackReason(back.getBackReason());
            orderDetailsService.updateById(orderDetails);
        }
        return true;
    }




}

